#include <Arduino.h>  // 必须放在最开头！

/* 
    按钮控制灯的闪烁
 */

// 定义led的gpio口
#define LED_PIN 18
#define BUTTON_PIN 4

int BUTTON_STATUS = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // 使用内部上拉电阻
}

void loop() {
  static bool lastButtonState = HIGH;  // 上一次按钮状态
  bool currentButtonState = digitalRead(BUTTON_PIN);  // 当前按钮状态

  if (currentButtonState == LOW && lastButtonState == HIGH) {
    // 按钮按下
    delay(50);  // 去抖动
    if (digitalRead(BUTTON_PIN) == LOW) {
      BUTTON_STATUS = !BUTTON_STATUS;  // 切换LED状态
      digitalWrite(LED_PIN, BUTTON_STATUS);
    }
  }

  lastButtonState = currentButtonState;  // 更新按钮状态
}